package clockwork;


import java.util.Observable; 
import java.util.Date;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import banking.BankImpl;


public class Clockwork extends Observable {

	
	
	private static Calendar calendar = Calendar.getInstance();
    Clock clock = new Clock();
    private static int TIMEMACHINE = 100;
    
    private static Clockwork theInstance;
    private Clockwork()  {}
    
    public static Clockwork getInstance()  {
        if (theInstance == null)
            theInstance = new Clockwork();
        return theInstance;
    }

	public void startTiming() {
		calendar.getTime();
		this.addObserver(new BankImpl("haha"));;
		Timer timer = new Timer(true);
		timer.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				try {
					clock.panel.repaint();
					int counter = 0;
					calendar.add(Calendar.DATE, ++counter);
					setChanged();
					notifyObservers();
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, 0, TIMEMACHINE);
	}
	
	public static long getTime(){
		Calendar timestamp = (Calendar) calendar.clone(); 
		long lTimestamp = timestamp.getTimeInMillis();
		return lTimestamp;
	}
	
	public static String getFormatedTime() {
		Date d = new Date();
        DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
        return df.format(d);
		
	}

}


